<?php

namespace app\api\controller;

use app\admin\model\department\Lists;
use app\admin\model\Dynamic;
use app\admin\model\product\Order;
use app\admin\model\Slide;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 获取配置信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfigs()
    {
        $name = $this->request->get('name');
        if (!$name) {
            $this->error('参数缺失！');
        }
        $config = Config::field('type,value,content')->where('name', $name)->find();
        if (!$config) {
            $this->error('该内容不存在！');
        }

        $this->success('配置信息为', ['type' => $config->type, 'value' => $config->value, 'content' => $config->content]);
    }

    /**
     * 首页轮播图
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSlides()
    {
        $type = input('type', 'index');
        $list = Slide::field('id,title,url,image')->where('status', 'normal')->where('type', $type)->order('id asc')->select();
        foreach ($list as &$val) {
            $val['image'] = make_full_url($val['image']);
        }
        $this->success('轮播图为', $list);
    }

    /**
     * 公告
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDynamics()
    {
        $list = Dynamic::field('id,content')->where('type', 1)->order('id desc')->select();
        $this->success('轮播图为', $list);
    }

    /**
     * 商品分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cates()
    {
        $list = \app\admin\model\product\Category::field('id,title,is_common')->where('status', 'normal')->order('sort asc,id desc')->select();
        $this->success('商品分类', $list);
    }

    /**
     * 获取店铺信息
     */
    public function getStoreInfo()
    {
        $storeAddress = config('site.store_address');
        $storeOwner = config('site.store_owner');
        $storeMobile = config('site.store_mobile');

        $this->success('店铺信息', ['address' => $storeAddress, 'owner' => $storeOwner, 'mobile' => $storeMobile]);
    }

    /**
     * 盈利排行榜
     */
    public function rankLists()
    {
        $month = strtotime(date('Y-m-01', time()));

        $today = strtotime('today');
        $yes = $today - 86400;
        $date = date('Y-m-d', time());
        $hour = strtotime($date . ' 12:00:00');

        $users = \app\admin\model\department\User::where('is_leader', 0)->where('status', 'normal')->select();
        $where['status'] = 20;
        if (time() < $hour) {
            $msg = '昨日';
            $where['updatetime'] = [['>=', $yes], ['<', $today]];
        } else {
            $msg = '今日';
            $where['updatetime'] = ['>=', $today];
        }

        $map = [
            'status' => 20,
            'send_time' => ['>=', $month]
        ];

        $data = [];
        foreach ($users as $val) {
            $departmentInfo = Lists::where('id', $val->department_id)->find();
            if ($departmentInfo->is_achievement != 1) {
                continue;
            }
            $where['department_user_id'] = $val->id;
            $listso = Order::where($where)->select();
            $total = 0.00;
            $profile = 0.00;
            $num = 0;
            foreach ($listso as $v) {
                $total += $v->total;
                $profile += round($v->total * $v->rate * 0.01, 2);
                $num += 1;
            }

            $map['department_user_id'] = $val->id;
            $month_profile = Order::where($map)->sum('profile');

            $item = [
                'username' => $val->username,
                'nickname' => $val->nickname,
                'department' => $departmentInfo->name,
                'num' => $num,
                'total' => $total,
                'profile' => $profile,
                'month_profile' => $month_profile
            ];
            $data[] = $item;
        }

        $profiles = array_column($data, 'profile');
        $month_profiles = array_column($data, 'month_profile');
        array_multisort($profiles, SORT_DESC, $month_profiles, SORT_DESC, $data);

        $this->success($msg, $data);
    }
}
